package blogcatback.config;

/**
 * @author Lijingjing
 * @date 2024/8/19 19:50
 * @description WebConfig
 */
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

@Configuration
public class WebConfig {

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true); // 是否允许发送Cookie
        config.addAllowedOriginPattern("*"); // 允许任何域名使用
        config.addAllowedHeader("*"); // 允许任何请求头
        config.addAllowedMethod("*"); // 允许任何HTTP方法（GET, POST 等）

        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
}